package pcjavanet.mobilesell.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import pcjavanet.mobilesell.model.Area;
import pcjavanet.mobilesell.model.AreaExample;
import pcjavanet.mobilesell.model.Member;
import pcjavanet.mobilesell.model.MemberExample;
import pcjavanet.mobilesell.model.MemberExample.Criteria;
import pcjavanet.mobilesell.model.PageBean;
import pcjavanet.mobilesell.model.QryCondition;
import pcjavanet.mobilesell.persistence.AreaMapper;
import pcjavanet.mobilesell.persistence.MemberMapper;


@Service
public class MemberService {


  @Autowired
  private  MemberMapper memberMapper;
  @Autowired
  private  AreaMapper areaMapper;


  public int  insert( Member member ) throws Exception{
	return memberMapper.insert (member);
  }


  public Member  selectByPrimaryKey( Member  bean ) {
 	int dbid = bean.getDbid();
	Member member = memberMapper.selectByPrimaryKey( dbid );
	Area area = areaMapper.selectByPrimaryKey( member.getRefAreaDbid() );
	member.setArea(area) ;
	return member ;
  }


  public int  updateByPrimaryKeySelective( Member member ) throws Exception{
	return memberMapper.updateByPrimaryKeySelective( member );
  }


  public int  deleteByPrimaryKey( Member  bean ) throws Exception{
 	int dbid = bean.getDbid();
	return memberMapper.deleteByPrimaryKey( dbid );
  }


  public List<Member>  selectByExample( Member member ) throws Exception{
    MemberExample example = new MemberExample();
    Criteria c = example.createCriteria();
    c.andMobileEqualTo( member.getMobile()  );
	return memberMapper.selectByExample( example );
  }

  public List<Member>  getOnePageBeans( QryCondition condition , PageBean pageBean ) throws Exception{
    MemberExample example = new MemberExample();
    ServiceUtil.dealCriteria( condition,  example.createCriteria() );
    
	List<Member> ls=memberMapper.getOnePageBeans( example , pageBean );
//begin
	if ( ls.size() > 0 ) {
  		Map<Integer,Integer> areaIdmap = new HashMap<Integer,Integer>();
  		for(Member  m : ls){
  			m.setArea(new Area ()) ;  
  			if ( m.getRefAreaDbid() != null ) 
  				areaIdmap.put(m.getRefAreaDbid(), m.getRefAreaDbid());
  		};
  		List<Integer> refAreaDbids = new ArrayList<Integer>();
  		Iterator<Integer> refAreaDbidit = areaIdmap.keySet().iterator(); 
  		while ( refAreaDbidit.hasNext() ){
  			refAreaDbids.add( refAreaDbidit.next() );
  		}
  		AreaExample areaExample = new AreaExample();
  		if ( refAreaDbids.size() > 0 ) 
  			areaExample.createCriteria().andDbidIn( refAreaDbids );
  		List<Area> areas = areaMapper.selectByExample( areaExample);
  		for(Member m : ls ){
  			for(Area c : areas ){
  				if ( m.getRefAreaDbid() !=null && c.getDbid() !=null && m.getRefAreaDbid().intValue() == c.getDbid().intValue() ) {
  					m.setArea( c );
  				}
  			}
  		}
	}
//end
	return ls;
  }

  public int getTotalRecords( QryCondition condition , PageBean pageBean ) throws Exception{
    MemberExample example = new MemberExample();
    ServiceUtil.dealCriteria( condition,  example.createCriteria() );
	return memberMapper.getTotalRecords( example , pageBean );
  }

  public int countByExample( Member member , PageBean pageBean ){
    MemberExample example = new MemberExample();
	return memberMapper.countByExample( example );
  }
  public List<Member> selectByExamplePage( Member member , PageBean pageBean ){
  MemberExample example = new MemberExample();
	return memberMapper.selectByExamplePage( example , pageBean );
  }

}
